#include "timer_hal.h"
#include "../pwm_data.h"
#include "../stm32files/inc/stm32f10x_rcc.h"
#include "../stm32files/inc/stm32f10x_tim.h"
#include "../stm32files/inc/stm32f10x_gpio.h"

void  setPWMEdges( int _base,int _arm1,int _arm2,int _jaw)
{
    TIM_SetCompare3(TIM2, _base);
    TIM_SetCompare4(TIM2, _arm1);
	  TIM_SetCompare3(TIM3, _arm2);
    TIM_SetCompare4(TIM3, _jaw);
}


static void initialiseChannels(TIM_TypeDef * timer)
{
			TIM_BDTRInitTypeDef tim_bdtr;
			TIM_OCInitTypeDef tim_oc;
 /* TIM1 channel 4 and 3 settings */
      tim_oc.TIM_OCMode = TIM_OCMode_PWM2;
			tim_oc.TIM_OutputState = TIM_OutputState_Enable;
			tim_oc.TIM_OutputNState = TIM_OutputNState_Enable;
			tim_oc.TIM_Pulse = PwmNewSettings( 0, 100);
			tim_oc.TIM_OCPolarity = TIM_OCPolarity_High;
			tim_oc.TIM_OCNPolarity = TIM_OCNPolarity_High;
			tim_oc.TIM_OCIdleState = TIM_OCIdleState_Set;
			tim_oc.TIM_OCNIdleState = TIM_OCNIdleState_Set;

      TIM_OC3Init(timer, &tim_oc);
      TIM_OC3PreloadConfig(timer, TIM_OCPreload_Enable);
      TIM_OC4Init(timer, &tim_oc);
      TIM_OC4PreloadConfig(timer, TIM_OCPreload_Enable);


       /* Automatic Output enable, break, dead time and lock configuration */
      tim_bdtr.TIM_OSSRState = TIM_OSSRState_Enable;
      tim_bdtr.TIM_OSSIState = TIM_OSSIState_Enable;
      tim_bdtr.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
      tim_bdtr.TIM_DeadTime = 10;
      tim_bdtr.TIM_Break = TIM_Break_Disable;
      tim_bdtr.TIM_BreakPolarity = TIM_BreakPolarity_High;
      tim_bdtr.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;

      TIM_BDTRConfig(timer, &tim_bdtr);

      TIM_CCPreloadControl(timer, ENABLE);


       /* timer enable counter */
       TIM_Cmd(timer, ENABLE);

      /* Main output enable */
      TIM_CtrlPWMOutputs(timer, ENABLE);

       /* Setting default state of pwm to pwm_off */
    //  pwm_off();

 //    TIM_ClearITPendingBit(timer, TIM_IT_COM);
			
}

void 
enablePwmOnTimer2()
{
	
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
 
    // Let PWM frequency equal 100Hz.
    // Let period equal 1000. Therefore, timer runs from zero to 1000. Gives 0.1Hz resolution.
    // Solving for prescaler gives 240.
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_TimeBaseStructInit( &TIM_TimeBaseInitStruct );
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_Period = TimerResolution;
    TIM_TimeBaseInitStruct.TIM_Prescaler = PrescalerResolution;
    TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStruct );
 
    TIM_OCInitTypeDef TIM_OCInitStruct;
    TIM_OCStructInit( &TIM_OCInitStruct );
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
    // Initial duty cycle equals 0%. Value can range from zero to 1000.
   
		GPIO_InitTypeDef gpio;
		gpio.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
		
		gpio.GPIO_Mode = GPIO_Mode_AF_PP;
    
		gpio.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &gpio);
	
	
		initialiseChannels(TIM2);
		TIM_CtrlPWMOutputs(TIM2, ENABLE);	

		TIM_Cmd(TIM2, ENABLE);
}

void 
enablePwmOnTimer3()
{

    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );
 
    // Let PWM frequency equal 100Hz.
    // Let period equal 1000. Therefore, timer runs from zero to 1000. Gives 0.1Hz resolution.
    // Solving for prescaler gives 240.
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_TimeBaseStructInit( &TIM_TimeBaseInitStruct );
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_Period = TimerResolution;
    TIM_TimeBaseInitStruct.TIM_Prescaler = PrescalerResolution;
    TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStruct );
 
    TIM_OCInitTypeDef TIM_OCInitStruct;
    TIM_OCStructInit( &TIM_OCInitStruct );
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
    // Initial duty cycle equals 0%. Value can range from zero to 1000.
		
/* GPIOA: TIM1 channel 1, 2 and 3 as alternate function push-pull */
	GPIO_InitTypeDef gpio;
		gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
		
		gpio.GPIO_Mode = GPIO_Mode_AF_PP;
    
		gpio.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &gpio);
	
		initialiseChannels(TIM3);
		
		TIM_CtrlPWMOutputs(TIM3, ENABLE);	
		
		TIM_Cmd(TIM3, ENABLE);
}
#ifdef NINO

/*
    NVIC_InitTypeDef nvic;
00073         GPIO_InitTypeDef gpio;
00074         TIM_TimeBaseInitTypeDef tim_base;
00075         TIM_OCInitTypeDef tim_oc;
00076         TIM_BDTRInitTypeDef tim_bdtr;
00077 
00078         (void)gpc_setup_reg(GPROT_PWM_OFFSET_REG_ADDR, &pwm_offset);
00079 

00081         RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |
00082                                RCC_APB2Periph_GPIOA |
00083                                RCC_APB2Periph_GPIOB, ENABLE);
00084 
00085         /* Enable TIM1 interrupt */
00086         nvic.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;
00087         nvic.NVIC_IRQChannelPreemptionPriority = 0;
00088         nvic.NVIC_IRQChannelSubPriority = 1;
00089         nvic.NVIC_IRQChannelCmd = ENABLE;
00090         NVIC_Init(&nvic);
00091 
00092         /* Enable TIM1 interrupt */
00093         nvic.NVIC_IRQChannel = TIM1_CC_IRQn;
00094         nvic.NVIC_IRQChannelPreemptionPriority = 0;
00095         nvic.NVIC_IRQChannelSubPriority = 1;
00096         nvic.NVIC_IRQChannelCmd = ENABLE;
00097         NVIC_Init(&nvic);
00098 
00099         /* GPIOA: TIM1 channel 1, 2 and 3 as alternate function
00100            push-pull */
00101         gpio.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
00102         gpio.GPIO_Mode = GPIO_Mode_AF_PP;
00103         gpio.GPIO_Speed = GPIO_Speed_50MHz;
00104         GPIO_Init(GPIOA, &gpio);
00105 
00106         /* GPIOB: TIM1 channel 1N, 2N and 3N as alternate function
00107          * push-pull
00108          */
00109         gpio.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
00110         GPIO_Init(GPIOB, &gpio);
00111 
00112         /* Time base configuration */
00113         tim_base.TIM_Period = PWM__BASE_CLOCK / PWM__FREQUENCY;
00114         tim_base.TIM_Prescaler = 0;
00115         tim_base.TIM_ClockDivision = 0;
00116         tim_base.TIM_CounterMode = TIM_CounterMode_Up;
00117         tim_base.TIM_RepetitionCounter = 0;
00118 
00119         TIM_TimeBaseInit(TIM1, &tim_base);
00120 
00121         /* TIM1 channel 1, 2 and 3 settings */
00122         tim_oc.TIM_OCMode = TIM_OCMode_Timing;
00123         tim_oc.TIM_OutputState = TIM_OutputState_Enable;
00124         tim_oc.TIM_OutputNState = TIM_OutputNState_Enable;
00125         tim_oc.TIM_Pulse = pwm_val;
00126         tim_oc.TIM_OCPolarity = TIM_OCPolarity_High;
00127         tim_oc.TIM_OCNPolarity = TIM_OCNPolarity_High;
00128         tim_oc.TIM_OCIdleState = TIM_OCIdleState_Set;
00129         tim_oc.TIM_OCNIdleState = TIM_OCNIdleState_Set;
00130 
00131         TIM_OC1Init(TIM1, &tim_oc);
00132         TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
00133         TIM_OC2Init(TIM1, &tim_oc);
00134         TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
00135         TIM_OC3Init(TIM1, &tim_oc);
00136         TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
00137 
00138         /* TIM1 configure channel 4 as adc trigger source */
00139         tim_oc.TIM_OCMode = TIM_OCMode_PWM2;
00140         tim_oc.TIM_Pulse = pwm_offset;
00141         TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
00142         TIM_OC4Init(TIM1, &tim_oc);
00143 
00144         /* Automatic Output enable, break, dead time and lock configuration */
00145         tim_bdtr.TIM_OSSRState = TIM_OSSRState_Enable;
00146         tim_bdtr.TIM_OSSIState = TIM_OSSIState_Enable;
00147         tim_bdtr.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
00148         tim_bdtr.TIM_DeadTime = 10;
00149         tim_bdtr.TIM_Break = TIM_Break_Disable;
00150         tim_bdtr.TIM_BreakPolarity = TIM_BreakPolarity_High;
00151         tim_bdtr.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
00152 
00153         TIM_BDTRConfig(TIM1, &tim_bdtr);
00154 
00155         TIM_CCPreloadControl(TIM1, ENABLE);
00156 
00157         /* Enable COM and CC interrupt */
00158         TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);
00159         //TIM_ITConfig(TIM1, TIM_IT_COM | TIM_IT_CC4, ENABLE);
00160 
00161         /* TIM1 enable counter */
00162         TIM_Cmd(TIM1, ENABLE);
00163 
00164         /* Main output enable */
00165         TIM_CtrlPWMOutputs(TIM1, ENABLE);
00166 
00167         /* Setting default state of pwm to pwm_off */
00168         pwm_off();


00211         TIM_ClearITPendingBit(TIM1, TIM_IT_COM);
00212 
00213         ON(LED_BLUE);
00214 
00215         TIM_SetCompare1(TIM1, pwm_val);
00216         TIM_SetCompare2(TIM1, pwm_val);
00217         TIM_SetCompare3(TIM1, pwm_val);
00218         TIM_SetCompare4(TIM1, pwm_offset);


#endif